<html>
<head>
<% jquery_path = File.expand_path(File.join(Redcar.root, %w(plugins html_view assets jquery-1.4.min.js))) %>
<script type="text/javascript" src="file://<%= jquery_path %>"></script>
<% json_path = File.expand_path(File.join(Redcar.root, %w(plugins html_view assets json2.js))) %>
<script type="text/javascript" src="file://<%= json_path %>"></script>

<% plugin_css = File.expand_path(File.join(plugin_root, %w(lib project_search stylesheets style.css))) %>
<style>
<% font_size = 12 %>
#results .text pre, #results .text pre span, #query,#results .line_num, #recent_queries {
  font-family: <%= Redcar::EditView.font %>;
  font-size: <%= font_size%>px;
}
#results .line_num {
  font-size:<%= font_size - 2 %>px;
  line-height:<%= font_size - 2 %>px;
}
#results .text pre, #results .text pre span {
  font-size: <%= font_size - 1 %>px;
  line-height: <%= font_size - 3 %>px;
}
</style>
<link rel="stylesheet" href="file://<%= plugin_css %>" type="text/css" media="screen">
</head>
<body>
<div id="search_form_container">
  <form id="search_form">
    <table>
      <tr class="input">
        <td class="label"><label for="query">Words:</label></td>
        <td class="field">
          <input id="query" type="text" value="<%= default_query %>"/>

          <!-- keeps a cache of the current query so if the user changes the query field, this remains the same as the results -->
          <input id="cached_query" type="hidden" />

          <div id="recent_queries" style="display: none;">
            <ul>
              <% (settings['recent_queries'] || []).each do |query| %>
                <li><%= CGI::escapeHTML(query) %></li>
              <% end %>
            </ul>
          </div>
        </td>
        <td class="expand">
          <input id="search" type="submit" value="Search" />
          <a href="#" title="Toggle Recent Queries" id="toggle_recent_queries">&#9660;</a>
        </td>
      </tr>
      <tr>
        <td></td>
        <td class="controls">
          <% if show_literal_match_option? %>
          <input type="checkbox" id="literal_match" <%="checked=checked" if @literal_match %>>
            <label for="literal_match">Literal Match</label>
          <% end %>
          <input type="checkbox" id="match_case" <%="checked=checked" if match_case? %>>
          <label for="match_case">Match case</label>
          <input type="checkbox" id="with_context" <%="checked=checked" if context? %>>
          <label for="with_context">With context</label>
        </td>
        <td></td>
      </tr>
    </table>
  </form>
</div>

<div id="results_container">
  <div id="results">
    <div id="no_results">
      Please provide a search term above.
    </div>
  </div>
</div>

<div id="results_summary" style="display: none;">
  <div class="text">
    <span id="line_results_count">0</span> lines matched in <span id="file_results_count">0</span> files
    <span style="float:right">&#9660;</span>
    <div id="files_nav_container" style="display:none">
      <ul id="files_nav">
      </ul>
    </div>
  </div>
</div>

<script type="text/javascript">
  $(document).ready(function() {
    $('#query').trigger('focus');

    $('#search_form').submit(function(ev) {
      ev.preventDefault();
      try {
        if ($("#literal_match").attr('checked')) { var literal_match = 'true'; } else { var literal_match = 'false'; }
        if ($("#match_case").attr('checked')) { var match_case = 'true'; } else { var match_case = 'false'; }
        if ($("#with_context").attr('checked')) { var with_context = 'true'; } else { var with_context = 'false'; }
        Controller.search($("#query").val(), literal_match, match_case, with_context);
      } catch(e) {
        alert(e.message);
      }
    });

    $('#query').keyup(function(ev) {
      if ($(this).val() == '') {
        $('input[type=submit]').attr('disabled', 'disabled');
      } else {
        $('input[type=submit]').attr('disabled', '');
      }
    });
    $('#query').trigger('keyup');

    $('#toggle_recent_queries').click(function(ev) {
      ev.preventDefault();
      $('#recent_queries').slideToggle('fast');
    });

    // in the case the user wants to search for things like <table>, we need to unescape the entities
    String.prototype.unescapeHTML = function() {
      var t = document.createElement('div');
      t.innerHTML = this;
      return t.firstChild.nodeValue;
    }

    $('#recent_queries li').click(function(ev) {
      ev.preventDefault();
      $('#query').val($(this).html().unescapeHTML());
      $('#query').trigger('keyup');
      $('#recent_queries').hide();
    });

    // need to use live here so that results added will be caught
    $('.result').live('click', function(ev) {
      ev.preventDefault();
      try {
        if ($("#literal_match").attr('checked')) { var literal_match = 'true'; } else { var literal_match = 'false'; }
        if ($("#match_case").attr('checked')) { var match_case = 'true'; } else { var match_case = 'false'; }
        Controller.openFile($(this).attr('data-href'), $(this).attr('data-line_num'), $("#cached_query").val(), literal_match, match_case);
      } catch(e) {
        alert(e.message);
      }
    });

    $('.text').click(function() {
      $('#files_nav_container').slideToggle();
    });

    $('.file_link').live('click',function() {
      $('body').animate({
          scrollTop: $("#"+$(this).attr('id').replace("link_","")).offset().top - 100
      }, 0);
    });
  });
</script>
</body>
</html>